# Direct outputs eligible for public release

# GLOBAL SETTINGS --------------------------------------------------------------

options(
    scipen = 999,
    digits = 16,
    max.print = .Machine$integer.max,
    show.error.locations = TRUE,
    warn = 1
)

RNGkind("L'Ecuyer-CMRG")
seed <- 818675309L
set.seed(seed) # setting main seed

# PACKAGES ---------------------------------------------------------------------

library(data.table)

# PACKAGE SETTINGS -------------------------------------------------------------

# data.table
setDTthreads(threads = 1)
options(datatable.print.class = TRUE, datatable.print.keys = TRUE)
# so that printing the data.table also shows the variable type on top

# BEGIN FILE -------------------------------------------------------------------

# Just in case something masks 'source()', use extra deliberate base::source().
base::source("~/code/0-utility-functions/table-and-text-utils.R", local = TRUE)

# READ IN OUR DATA -------------------------------------------------------------

# Decomposition inputs: Winner Wage Earnings

db_w2_wages_int_ext_dt <-
    readRDS(
        "~/estimation-output/intensive_extensive_decomp_inputs_db_w2_wages.rds"
    )
db_w2_wages_int_ext_dt <-
    db_w2_wages_int_ext_dt[
        ,
        .(
            ref_onset_time,
            effect_time,
            income_quartile,
            conditional_earnings_post_treated,
            conditional_earnings_pre_treated,
            conditional_earnings_post_control,
            conditional_earnings_pre_control,
            ext_marg_var_post_treated,
            ext_marg_var_pre_treated,
            ext_marg_var_post_control,
            ext_marg_var_pre_control,
            catt_treated_unique_units
        )
    ]
setnames(db_w2_wages_int_ext_dt, "effect_time", "ref_event_time")
db_w2_wages_int_ext_dt[, ref_onset_time := as.integer(ref_onset_time)]

# Decomposition inputs: Per-Adult Total Labor Earnings

per_adult_total_empl_income_int_ext_dt <-
    readRDS(
        "~/estimation-output/intensive_extensive_decomp_inputs_per_adult_total_empl_income.rds" #nolint
    )
per_adult_total_empl_income_int_ext_dt <-
    per_adult_total_empl_income_int_ext_dt[
        ,
        .(
            ref_onset_time,
            effect_time,
            income_quartile,
            conditional_earnings_post_treated,
            conditional_earnings_pre_treated,
            conditional_earnings_post_control,
            conditional_earnings_pre_control,
            ext_marg_var_post_treated,
            ext_marg_var_pre_treated,
            ext_marg_var_post_control,
            ext_marg_var_pre_control,
            catt_treated_unique_units
        )
    ]
setnames(
    per_adult_total_empl_income_int_ext_dt,
    "effect_time",
    "ref_event_time"
)
per_adult_total_empl_income_int_ext_dt[
    ,
    ref_onset_time := as.integer(ref_onset_time)
]

# For each outcome and quartile:
# - calculate the ext / int effects and extensive-margin share for each (w, ℓ)
# - calculate the cohort-weighted-average extensive-margin share for each ℓ
# - calculate equally-weighted average across ℓ (+1 to +5)
# ^ matches our aggregate estimates

db_w2_wages_int_ext_dt[, dependent_var := "db_w2_wages"]
per_adult_total_empl_income_int_ext_dt[
    , dependent_var := "per_adult_total_empl_income"
]

quartile_decomp_data_wl <-
    rbindlist(
        list(db_w2_wages_int_ext_dt, per_adult_total_empl_income_int_ext_dt),
        use.names = TRUE
    )

# Housekeeping
db_w2_wages_int_ext_dt <- NULL
per_adult_total_empl_income_int_ext_dt <- NULL
rm(db_w2_wages_int_ext_dt, per_adult_total_empl_income_int_ext_dt)

quartile_decomp_data_wl[
    ,
    ext_effect :=
   (
    ((ext_marg_var_post_treated - ext_marg_var_pre_treated) - (ext_marg_var_post_control - ext_marg_var_pre_control)) #nolint
    * conditional_earnings_post_control
   )
]

quartile_decomp_data_wl[
    ,
    int_effect :=
    (
        ((conditional_earnings_post_treated - conditional_earnings_pre_treated) - (conditional_earnings_post_control - conditional_earnings_pre_control)) #nolint
        * ext_marg_var_post_treated
    )
]

quartile_decomp_data_wl[
    ,
    ext_share := (ext_effect / (ext_effect + int_effect))
]

quartile_decomp_data_l <-
    quartile_decomp_data_wl[
        ,
        .(ext_share = weighted.mean(ext_share, w = catt_treated_unique_units)),
        by = .(ref_event_time, income_quartile, dependent_var)
    ]

quartile_decomp_data_avg <-
    quartile_decomp_data_wl[
            ,
            .(ext_share = mean(ext_share)),
            by = .(income_quartile, dependent_var)
    ]

# aggregate across quartiles
aggregate_decomp_data_avg <-
    quartile_decomp_data_avg[
        ,
        .(ext_share = mean(ext_share)),
        by = .(dependent_var)
    ]
aggregate_decomp_data_avg[, income_quartile := 5]

decomp_data_avg <-
    rbindlist(
        list(quartile_decomp_data_avg, aggregate_decomp_data_avg),
        use.names = TRUE
    )[order(dependent_var, income_quartile)]

# Housekeeping
quartile_decomp_data_wl <- NULL
quartile_decomp_data_l <- NULL
quartile_decomp_data_avg <- NULL
aggregate_decomp_data_avg <- NULL
rm(
    quartile_decomp_data_wl,
    quartile_decomp_data_l,
    quartile_decomp_data_avg,
    aggregate_decomp_data_avg
)

# MAKE TABLE -------------------------------------------------------------------

filedir <- "~/paper/tables/table-A.3.tex"
file.create(filedir)

# Start Table A.3
start_latex_table(
    column_structure = "cccccccc",
    file_dir = filedir,
    append = TRUE
)
add_latex_table_rule(rule_type = "toprule", file_dir = filedir, append = TRUE)
add_latex_table_rule(rule_type = "midrule", file_dir = filedir, append = TRUE)

heading_text_input1 <-
    " &  & \\multicolumn{6}{c}{Sample} "
write_latex_table_row(heading_text_input1, file_dir = filedir, append = TRUE)

add_latex_cmidrule(
    point_start_col = "l",
    point_end_col = "r",
    start_col = 3,
    end_col = 8,
    file_dir = filedir,
    append = TRUE
)

heading_text_input2 <-
    " \\multirow{3}{*}{Outcome} & & Quartile 1  &  Quartile 2 & Quartile 3 & Quartile 4 & & \\multirow{2}{*}{Aggregate}" #nolint
write_latex_table_row(heading_text_input2, file_dir = filedir, append = TRUE)

heading_text_input3 <-
    "  & & Pre-Win Income  & Pre-Win Income & Pre-Win Income & Pre-Win Income & & " #nolint
write_latex_table_row(heading_text_input3, file_dir = filedir, append = TRUE)

heading_text_input4 <-
    "  & & (1) & (2) & (3) & (4) & & (5) " #nolint
write_latex_table_row(heading_text_input4, file_dir = filedir, append = TRUE)

heading_text_input1 <- NULL
heading_text_input2 <- NULL
heading_text_input3 <- NULL
heading_text_input4 <- NULL
rm(
    heading_text_input1,
    heading_text_input2,
    heading_text_input3,
    heading_text_input4
)

add_latex_table_rule(rule_type = "midrule", file_dir = filedir, append = TRUE)

# Add body of Table A.3
# - introduce convenience functions given a similar structure throughout

TableA3Row <- function(dt, text) {
    dt_temp <- copy(dt)
    dt_input <-
        sprintf(
            "%s & & %s & %s & %s & %s & & %s",
            text,
            fmt_fixed_decimal(
                input = dt_temp[income_quartile == 1]$ext_share,
                sub_for_missing = "-",
                should_be_rounded = TRUE,
                round_digits = 2,
                decimal_digits = 2,
                scale_factor = 1,
                add_comma = TRUE
            ),
            fmt_fixed_decimal(
                input = dt_temp[income_quartile == 2]$ext_share,
                sub_for_missing = "-",
                should_be_rounded = TRUE,
                round_digits = 2,
                decimal_digits = 2,
                scale_factor = 1,
                add_comma = TRUE
            ),
            fmt_fixed_decimal(
                input = dt_temp[income_quartile == 3]$ext_share,
                sub_for_missing = "-",
                should_be_rounded = TRUE,
                round_digits = 2,
                decimal_digits = 2,
                scale_factor = 1,
                add_comma = TRUE
            ),
            fmt_fixed_decimal(
                input = dt_temp[income_quartile == 4]$ext_share,
                sub_for_missing = "-",
                should_be_rounded = TRUE,
                round_digits = 2,
                decimal_digits = 2,
                scale_factor = 1,
                add_comma = TRUE
            ),
            fmt_fixed_decimal(
                input = dt_temp[income_quartile == 5]$ext_share,
                sub_for_missing = "-",
                should_be_rounded = TRUE,
                round_digits = 2,
                decimal_digits = 2,
                scale_factor = 1,
                add_comma = TRUE
            )
        )
    write_latex_table_row(
        dt_input,
        file_dir = filedir,
        append = TRUE
    )
    invisible(dt_input)
}

# Winner Wage Earnings
TableA3Row(
    dt = decomp_data_avg[dependent_var == "db_w2_wages"],
    text = "Winner Wage Earnings"
)

# Per-Adult Total Labor Earnings
TableA3Row(
    dt = decomp_data_avg[dependent_var == "per_adult_total_empl_income"],
    text = "Per-Adult Total Labor Earnings"
)

# End Table A.3
add_latex_table_rule(
  rule_type = "bottomrule",
  file_dir = filedir,
  append = TRUE
)
end_latex_table(file_dir = filedir, append = TRUE)
filedir <- NULL
rm(filedir)

# Housekeeping
decomp_data_avg <- NULL
rm(decomp_data_avg)